<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: wsh
 * @Date: 2024-05-31 10:33:30
 * @LastEditors: wsh
 * @LastEditTime: 2024-06-03 14:57:53
-->
<template>
  <div>
    <AplidForm ref="formRef" v-bind="formConfig" v-model="formModel" @submit="handleSubmit" />
  </div>
</template>
  
<script setup lang="ts">
import { ref, computed } from 'vue';
import apiMod from '@/apiMod'
import { processStatusConfig, estimateResultConfig, transformResultConfig, checkResultConfig } from '../config'
let emit = defineEmits(['refresh'])

type Props = {
  nowRowData?: any
}
let props = withDefaults(defineProps<Props>(), {
  nowRowData: {},
})
console.log(props.nowRowData);

const formModel = ref({ ...props.nowRowData });
const formRef = ref();
const str = {
  processStatus: props.nowRowData.processStatus == 0,//未审批
  estimateResult: props.nowRowData.processStatus == 1 && (!props.nowRowData.estimateResult || props.nowRowData.estimateResult != 3),
  transformResult: props.nowRowData.estimateResult == 3 && (!props.nowRowData.transformResult || props.nowRowData.transformResult != 3),
  checkResult: props.nowRowData.transformResult == 3 && (!props.nowRowData.checkResult || props.nowRowData.checkResult != 3),
}
const formConfig = computed(() => {
  const k: any = Object.values(str).map((item, index) => {
    if (item) {
      return Object.keys(str)[index]
    }
  }).filter(Boolean)[0]

  const config = {
    processStatus: processStatusConfig,
    estimateResult: estimateResultConfig,
    transformResult: transformResultConfig,
    checkResult: checkResultConfig,
  }
  return config[k]
})

// // 计算当前表单的配置
// const currentFormConfig = computed(() => {
//   switch (true) {
//     case props.nowRowData.processStatus == 0:
//       return processStatusConfig;
//     case props.nowRowData.processStatus == 1 && !props.nowRowData.estimateResult:
//       return estimateResultConfig;
//     case props.nowRowData.estimateResult == 3 && !props.nowRowData.transformResult:
//       return transformResultConfig;
//     case props.nowRowData.transformResult == 3 && !props.nowRowData.checkResult:
//       return checkResultConfig;
//     default:
//       return {};
//   }
// });

const handleSubmit = async (params, done) => {
  try {
    await apiMod.view_bscBusinessProcess_edit({ ...props.nowRowData, ...params });
    done();
    emit('refresh');
  } catch (error) {
    console.error('Error submitting form:', error);
  }
};

const onSave = () => {
  formRef.value.onSubmit();
};

defineExpose({
  onSave,
});
</script>

<style scoped></style>
